$(function () {

//操作时间
    $('#time_start').daterangepicker({
        "singleDatePicker": true,
        "timePicker": true,
        "timePicker24Hour": true,
        "timePickerSeconds": true,
        "parentEl": "200",
        locale: {
            format:'YYYY-MM-DD HH:mm:ss',
            applyLabel: '确认',
            cancelLabel: '取消',
            weekLabel: 'W',
            customRangeLabel: 'Custom Range',
            daysOfWeek:["日","一","二","三","四","五","六"],
            monthNames: ["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"]
        }
    }, function(start, end, label) {
        $('#time_start').val(start.format('YYYY-MM-DD HH:mm:ss'));
    });
    $("#time_start").val("");//清空控件默认时间

    $('#time_end').daterangepicker({
        "singleDatePicker": true,
        "timePicker": true,
        "timePicker24Hour": true,
        "timePickerSeconds": true,
        "parentEl": "200",
        locale: {
            format:'YYYY-MM-DD HH:mm:ss',
            applyLabel: '确认',
            cancelLabel: '取消',
            weekLabel: 'W',
            customRangeLabel: 'Custom Range',
            daysOfWeek:["日","一","二","三","四","五","六"],
            monthNames: ["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"]
        }
    }, function(start, end, label) {
        $('#time_end').val(start.format('YYYY-MM-DD HH:mm:ss'));
    });
    $('#time_end').val("");//清空控件默认时间

    var cTimeS_hidden = $("#cTimeS_hidden").val(), cTimeE_hidden = $("#cTimeE_hidden").val();
    if(cTimeS_hidden != undefined && cTimeS_hidden != ""){
        $("#time_start").val(cTimeS_hidden);
    }
    if(cTimeE_hidden != undefined && cTimeE_hidden != "" && cTimeE_hidden != null){
        $("#time_end").val(cTimeE_hidden);
    }

    //加载异步数据（获取收货地点和出货地点）
    $.get("/getDepotList", function (re) {
        var depotList = re.data;
        var depotOptions = "";
        for (var i in depotList) {
            var depot = depotList[i];
            depotOptions += "<option value='" + depot.id + "'>" + depot.name + "</option>";
        }
        $("#depot").append(depotOptions);

        var depot=$("#depot_hidden").val();
        if(depot){
            $("#depot").find("option[value='"+depot+"']").attr("selected",true);
        }

    });



    $('#bot_detail').on('show.bs.modal', function (event) {

        $("#detail_model").html("");
        var _e = $(event.relatedTarget);
        var bQrcodeId = _e.data("box"),companyId=_e.data("company");
        $.get("/getBottleByBox?bQrcodeId=" + bQrcodeId + "&companyId=" + companyId, function (re) {
            var list = re.data;
            var str = "";
            for (var i in list) {
                var box = list[i];
                str += "<tr><th>"+box.proName+"</th>"
                    +"<th>"+box.bQrcodeId+"</th>"
                    +"<th>"+box.pQrcodeId+"</th>"
                    +"<th>"+box.depotName+"</th>"
                    +"<th>"+getFormatDateByLong(box.ts,"yyyy-MM-dd hh:mm:ss")+"</th>"
                    +"<th>"+box.cAdmin+"</th></tr>";
            }
            $("#detail_model").html(str);
        });
    });

    $('#tray_detail').on('show.bs.modal', function (event) {

        $("#detail_model").html("");
        var _e = $(event.relatedTarget);
        var trayQrcode = _e.data("tray");
        $.get("/getBoxByTray?trayQrcode=" + trayQrcode, function (re) {
            var list = re.data;
            var str = "";
            for (var i in list) {
                var box = list[i];
                str += "<tr><th>"+box.proName+"</th>"
                    +"<th>"+box.bQrcodeId+"</th>"
                    +"<th>"+box.depotName+"</th>"
                    +"<th>"+box.bingCompanyName+"</th>"
                    +"<th>"+getFormatDateByLong(box.ts,"yyyy-MM-dd hh:mm:ss")+"</th>"
                    +"<th>"+box.cAdmin+"</th></tr>";
            }
            $("#detail_model").html(str);
        });
    });



    //数据导出
    $("#btn_export_box").click(function () {
        var cTimeS = $("#time_start").val();
        var cTimeE = $("#time_end").val();
        var depot = $("#depot").val();
        var cAdmin = $("#cAdmin").val();
        //有数据时才能导出
        if($("#resultTotal_hidden").val() != 0){
            bootbox.bconfirm("确定是否导出？", function (re) {
                if (re) {
                    var url="/relateBoxExport?cTimeS="+cTimeS+"&cTimeE="+cTimeE+"&depot="+depot+"&cAdmin="+cAdmin;
                    window.open(url);
                }
            });
        }else{
            bootbox.balert("没有数据，无法导出!");
        }
    });

    //数据导出
    $("#btn_export_tray").click(function () {
        var cTimeS = $("#time_start").val();
        var cTimeE = $("#time_end").val();
        var depot = $("#depot").val();
        var cAdmin = $("#cAdmin").val();
        //有数据时才能导出
        if($("#resultTotal_hidden").val() != 0){
            bootbox.bconfirm("确定是否导出？", function (re) {
                if (re) {
                    var url="/relateTrayExport?cTimeS="+cTimeS+"&cTimeE="+cTimeE+"&depot="+depot+"&cAdmin="+cAdmin;
                    window.open(url);
                }
            });
        }else{
            bootbox.balert("没有数据，无法导出!");
        }
    });
});





/**
 *转换long值为日期字符串
 * @param l long值
 * @param pattern 格式字符串,例如：yyyy-MM-dd hh:mm:ss
 * @return 符合要求的日期字符串
 */

function getFormatDateByLong(l, pattern) {
    return getFormatDate(new Date(l), pattern);
}
/**
 *转换日期对象为日期字符串
 * @param l long值
 * @param pattern 格式字符串,例如：yyyy-MM-dd hh:mm:ss
 * @return 符合要求的日期字符串
 */
function getFormatDate(date, pattern) {
    if (date == undefined) {
        date = new Date();
    }
    if (pattern == undefined) {
        pattern = "yyyy-MM-dd hh:mm:ss";
    }
    return date.format(pattern);
}
//扩展Date的format方法
Date.prototype.format = function (format) {
    var o = {
        "M+": this.getMonth() + 1,
        "d+": this.getDate(),
        "h+": this.getHours(),
        "m+": this.getMinutes(),
        "s+": this.getSeconds(),
        "q+": Math.floor((this.getMonth() + 3) / 3),
        "S": this.getMilliseconds()
    }
    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    for (var k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
        }
    }
    return format;
}